Assembly Language
© Copyright Brian Brown, 1988-2000. All rights reserved.
| Notes | Home Page |


ASSEMBLY LANGUAGE PROGRAMMING, Part 3
home page prev page next page


IMPLEMENTATION OF HIGH LEVEL LANGUAGE CONSTRUCTS
In High Level Languages such as PASCAL and BASIC, several constructs are available which help to implement programs. You should know how these constructs are implemented in assembly language.The constructs that we will now deal with involve SELECTION and ITERATION. Both types of constructs are implemented using the conditional BRANCH instructions of the processor.

These types of instructions test the state of the various flags of the status register. All variables are memory based. Any manipulation of variables normally involves three steps,

  1. Load the variable into a register
  2. Perform the operation
  3. Store the result back into the variables location

6802 Processor Examples

  1. The IF statement
    In comparing the value of operands, consider the following example.
    
    		 IF X = 2 THEN Y = X
    
    

    The compare statement must be coded in such a way as to compare the value of X against the constant 2. As the variable X is stored in memory, the programmer should first load a register with the variable X before making the comparison (because most processors do not support a compare between memory contents and immediate data).This example gets coded as,

    
    		X:	DFB	10
    		Y:	DFB	00
    			....
    			LDAA	X 	; load A acc with value of X
    			CMPA	#02	; compare A acc with immediate data 
    			BNE	IF1 	; exit if false 
    			LDAA 	X 	; get value of X 
    			STAA	Y 	; store value of X at variable Y
    		IF1: 	..... 		; next statement after if construct
    
    

    Lets consider another example.

    
    		IF X = Y THEN Y = 0
    

    In this case, the code to be generated by the assembler for the compare statement depends upon the addressing modes available. The options available are,

    
    		memory to memory compare
    		CMP [X], [Y]
    		register to memory compare
    		CMPA [Y] 
    		register to register compare
    		CMPAB
    

    Both X and Y variables are memory based, so if the processor supports a comparison of two memory operands, it could be coded as,

    
    		CMP [X],[Y] 	; sample only
    

    However, most processors do not support this. The most common option is the comparison of a register variable against memory contents. This is coded as follows,

    
    		LDAA 	X 	; get variable X 
    		CMPA 	Y 	; compare with variable Y 
    		BNE 	IF1 	; exit it not equal 
    		LDAA 	#00H 	; set variable Y to zero
    		STAA 	Y	
    	IF1: 	....
    
    

    This code can clearly be optimized (ie, some instructions can be removed without affecting the original intent of the code). So far we have considered comparisons for equality. The conditional branch instruction will vary depending upon what type of comparison test is used. The following tables illustrate common comparison tests and their associated conditional branch instructions.

    
    		+-----------------+-------------+--------------+
    		| Signed Operands | Branch True | Branch False |
    		+-----------------+-------------+--------------+
    		| r > m           |    BGT      |     BLE      | 
    		+-----------------+-------------+--------------+
    		| r >=m           |    BGE      |     BLT      | 
    		+-----------------+-------------+--------------+
    		| r = m           |    BEQ      |     BNE      |
    		+-----------------+-------------+--------------+
    		| r <=m           |    BLE      |     BGT      |
    		+-----------------+-------------+--------------+
    		| r < m           |    BLT      |     BGE      |
    		+-----------------+-------------+--------------+
    
    		If ....... Then ---- Use Branch False 
    		If NOT ... Then ---- Use Branch True 
    
    		+-----------------+-------------+--------------+
    		|UnSigned Operands| Branch True | Branch False |
    		+-----------------+-------------+--------------+
    		| r > m           |    BHI      |     BLS      |
    		+-----------------+-------------+--------------+
    		| r >=m           |    BCC      |     BCS      |
     		+-----------------+-------------+--------------+
    		| r = m           |    BEQ      |     BNE      | 
    		+-----------------+-------------+--------------+
    		| r <=m           |    BLS      |     BHI      |
    		+-----------------+-------------+--------------+
    		| r < m           |    BCS      |     BCC      |
    		+-----------------+-------------+--------------+
    
    

    The following table represents a cross-reference between branch instructions and the flags they test.

    
    		+------+----------------+----------+
    		| 6802 | Flags Tested   |     8088 | 
    		+------+----------------+----------+
    		| BCC  | C = 0          | JNB, JAE | 
    		+------+----------------+----------+
    		| BCS  | C = 1          | JB, JNAE |
    		+------+----------------+----------+
    		| BNE  | Z = 0          | JNE, JNZ | 
    		+------+----------------+----------+
    		| BEQ  | Z = 1          | JE, JZ   |
    		+------+----------------+----------+
    		| BPL  | N = 0          | JNS      |
    		+------+----------------+----------+
    		| BMI  | N = 1          | JS       |
    		+------+----------------+----------+ 
    		| BHI  | C + Z = 0      | JNBE, JA |
    		+------+----------------+----------+ 
    		| BLS  | C + Z = 1      | JBE, JNA |
    		+------+----------------+----------+
    		| BGE  | N EOR V = 0    | JNL, JGE | 
    		+------+----------------+----------+
    		| BLT  | N EOR V = 1    | JL, JNGE | 
    		+------+----------------+----------+
    		| BGT  |Z + (N EOR V)= 0| JG, JNLE | 
    		+------+----------------+----------+
    		| BLE  |Z + (N EOR V)= 1| JLE,JNG  | 
    		+------+----------------+----------+
    
    

    These tables are useful in determining the correct conditional instruction to use for a particular comparison on specific data types. Coding the following statement applicable to two unsigned 8bit data values

    
    		IF X <= Y THEN Y = 4 
    
    		X:	DFB	10H
    		Y:	DFB	12H
    
    		IF:	LDAA	X
    			CMPA	Y 
    			BHI 	IF1
    			LDAA 	#04H 
    			STAA 	Y 
    		IF1:	....
    
    
  2. The IF THEN ELSE statement
    In comparing the value of operands, consider the following example.
    
    		IF X = 2 THEN Y = X ELSE X = Y
    

    This becomes coded as,

    
    		X:	DFB	00
    		Y: 	DFB 	00 
    		IF: 	LDAA 	X 
    			CMPA 	#02D 
    			BNE 	ELSE1 
    			LDAA	X 
    			STAA 	Y 
    			JMP 	IF1
    		ELSE1: 	LDAA 	Y 
    			STAA 	X 
    		IF1: 	....
    
    
  3. The WHILE WEND statement
    Consider the following example for unsigned values.
    
    		WHILE X < 10 DO
    			Y = Y + 1 X = X + 1 
    		WEND
    

    This becomes coded as,

    
    		X:	DFB	00H 
    		Y: 	DFB	00H
    		DO1: 	LDAB 	X 
    			CMPB 	#10D 
    			BCC 	EXIT1 	; for signed use BLT
    			LDAA 	Y	; increment value of Y 
    			ADDA 	#01 
    			STAA 	Y 
    			LDAA 	X 	; increment value of X 
    			ADDA 	#01 
    			STAA 	X 
    			JMP 	DO1
    		EXIT1: 	...
    
    

    Consider the coding of the following HLL program into 6802 assembler.

    
    	Program HLLTest();
    	var loop, val1, val2 : Byte;
    	Begin 
    		val1 := 0; 
    		val2 := 0; 
    		loop := 0; 
    		while loop <= 10 do 
    		begin 
    			val2 := val2 + loop;
    			loop := loop + 1 
    		end; 
    		if val1 < val2 then val1 := val2 
    		else val2 := val1 
    	end.
    
    

    The 6802 assembler version is

    
    		; HLLtest.asm
    		CPU	6802 
    		HOF 	MOT 
    		ORG 	100h 
    		loop: 	dfb	0 
    		val1: 	dfb 	0 
    		val2: 	dfb 	0
    		ORG 	120h 
    
    		Begin: 	LDAA 	#0 	; val1 := 0 
    			STAA 	val1 
    			LDAA 	#0 	; val2 := 0 
    			STAA 	val2 
    			LDAA 	#0 	; loop := 0 
    			STAA 	loop 
    		While: 	LDAA 	loop 	; while loop <= 10 do 
    			CMPA 	#10 
    			BGT 	if1 
    			LDAA 	val2 	; val2 := val2 + loop
    			LDAB 	loop 
    			ABA
    			STAA 	val2 
    			LDAA 	loop 	; loop := loop + 1 
    			ADDA 	#01
    			STAA 	loop 
    			JMP 	While 	; endwhile 
    		if1: 	LDAA 	val2 	; if val1 < val2 then 
    			CMPA 	val1 
    			BGE 	Else 
    			LDAA 	val2 	; val1 := val2 
    			STAA 	val1 
    			JMP	endif 
    		Else: 	LDAA 	val1 	; else val2 := val1 
    			STAA 	Val2 
    		Endif: 	NOP
    			SWI
    			End	Begin
    
    

home page prev page next page
© Copyright Brian Brown, 1988-2000. All rights reserved.